<?php

namespace Admin\Controller;
use Think\Controller;

class UserRoleController extends Controller{

	/*
		给用户添加一个或多个角色
	*/
	public function add(){

		$this->isAjaxLogin();//检查是否登录


		$uid=2;//I('post.uid');//用户id
		$rids=[2,3,4];//I('post.rid');//角色id
		
		$model=M('user_role');
        $rid=implode(',', $rids);
            $result=$model->add([
                        'uid'=>$uid,
                        'rid'=>$rid,
                    ]); 
            if($result){
                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
            }else{
                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] =  "添加失败";
            }
        
        $this->ajaxReturn($ajaxReturn);       
          
	}

	/*
	删除用户
	*/

	// public function delete(){

	// 	$this->isAjaxLogin();//检查是否登录

 //        $id = I('post.id/d');

 //        $model = M('admin_user');

 //        $model->where('id='.$id)->delete();
        
 //        $ajaxReturn['code'] = 1;
 //        $ajaxReturn['msg']  = "SUCCESS";

 //        $this->ajaxReturn($ajaxReturn);
	// }

	/*
	修改用户
	*/
	// public function update(){
	// 	$this->isAjaxLogin();//检查是否登录

	// 	$model=D('admin_user');

	// 	$name=I('post.name');//用户名
	// 	$password=I('post.password');//密码
	// 	$phone=I('post.phone');//手机号
	// 	$sign=I('post.sign');//用户状态

	// 	$id=I('post.id');

	// 	$data['name']   = $name;
 //        $data['password']   = $password;
 //        $data['phone']   = $phone;
 //        $data['sign'] = $sign;
 //        if($model->validate($model->_update_validate)->create($data) &&$model->where('id='.$id)->save($data)){
 //        	$ajaxReturn['code'] = 1;
 //            $ajaxReturn['msg'] = "SUCCESS";
 //        }else{
 //        	$ajaxReturn['code'] = 2;
 //            $ajaxReturn['msg'] =  $model->getError();
 //        }
 //        $this->ajaxReturn($ajaxReturn);
	// }

	/*
	用户分配角色列表的展示
	*/

	public function lists(){
		$this->isAjaxLogin();//检查是否登录

        $model = M('user_role');

        $result =$model->select();

        if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

    /*
    删除用户所分配的角色
    */
    public function delete(){
        $this->isAjaxLogin();

        $uid = 2;//I('post.id/d');
        $rids = 4;//I('post.id/d');

        $model = M('user_role');

        $result=$model->where('uid='.$uid && 'rid='.$rids )->delete();
        if($result){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = "数据删除失败";
        }
        
        $this->ajaxReturn($ajaxReturn);
    }
}